/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package control;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.TreeSet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import modele.Departement;
import modele.Medecin;
import modele.Pays;
import modele.Specialite;

/**
 *
 * @author gcanu
 */
public class Controleur extends HttpServlet {

    private Pays p;

    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        p = new Pays();
    }

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String action = request.getParameter("action");
        String dep = request.getParameter("dep");
        String nomMedecin = request.getParameter("nomMedecin");
        String nomSpe = request.getParameter("nomSpe");
        String page;

        if (action == null) {
            page = "index.jsp";
        } else if (action.equals("listeParDep")) {
            if (dep == null) {
                Collection<Departement> ld = p.getLesDeps();
                request.setAttribute("listeD", ld);
                page = "listeDepartement.jsp";
            } else {
                Collection<Medecin> lm = p.getLeDep(dep).getLesMeds();
                request.setAttribute("listeM", lm);
                request.setAttribute("numDep", dep);
                page = "listeMedecin.jsp";
            }
        } else if (action.equals("listeParNom")) {
//            Collection<Medecin> lm = p.getLeDep(dep).getLesMeds();
//            request.setAttribute("listeM", lm);

            if (nomMedecin == null) {
                page = "rechercheMedecin.jsp";
            } else {
                Collection<Medecin> lm = new TreeSet<Medecin>();
                for (Departement unDep : p.getLesDeps()) {
                    Collection ld = unDep.getLesMedsParNom(nomMedecin);
                    lm.addAll(ld);
                }
                request.setAttribute("listeMed", lm);
                request.setAttribute("nomMed", nomMedecin);
                page = "listeMedParNom.jsp";
            }
        } else if (action.equals("listeParSpe")) {

            if (nomSpe == null) {
                Collection<Specialite> ls = p.getLesSpes();
                request.setAttribute("listeS", ls);
                page = "listeSpecialite.jsp";
            } 
            else {
                Collection<Medecin> lm = p.getLaSpe(nomSpe).getLesMeds();
                request.setAttribute("listeM", lm);
                request.setAttribute("nomSpe", nomSpe);
                page = "listeMedParSpe.jsp";
            }
        } else {
            page = "index.jsp";
        }
        request.getRequestDispatcher(page).forward(request, response);
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
